/**
 * DataSetDialog.java : [com.shift.beaver2.metafile.editors] Created on 2009. 1. 20.
 * 
 * Copyright (c) 2000-2008 Shift Information & Communication Co.
 * 5F, Seongsu Venture town, 231-1, Seongsu2-Ga Seongdong-Gu, Seoul, Korea 133-826
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Shift Information & Communication Co. ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use 
 * it only in accordance with the terms of the license agreement you 
 * entered into with Shift Information & Communication.
 */
package com.shift.converter.editors;

import java.net.MalformedURLException;
import javax.swing.text.ChangedCharSetException;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.shift.converter.util;

public class URLDialog extends Dialog {

	public String type = "";
	public String charset = "";
	public String name = "";
	public String url = "";

	private Combo typeCombo = null;
	private Combo charsetCombo = null;
	private Text nameText = null;
	private Text urlText = null;
	private Button testConnectionButton = null;

	private final String[] TYPE_ITEMS = {"XML", "JSON"};
	private final String DEFAULT_CHARSET = "UTF-8";

	public URLDialog(Shell parentShell) {
		super(parentShell);
	}
	
	private void initData() {
		if(type.equals(""))
			typeCombo.setText(TYPE_ITEMS[0]);
		else typeCombo.setText(type);
		if(charset.equals(""))
			charsetCombo.setText(DEFAULT_CHARSET);
		else charsetCombo.setText(charset);
		nameText.setText(name);
		urlText.setText(url);
	}
	
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.getShell().setText("Add URL");
		final GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		container.setLayout(layout);
		
		final Label typeLabel = new Label(container, SWT.NONE);
		GridData gd = new GridData(GridData.FILL);
		typeLabel.setText("Type :");
		typeLabel.setLayoutData(gd);
		
		// DataSource Type
		typeCombo = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		gd = new GridData(GridData.FILL);
		gd.horizontalSpan = 2;
		gd.widthHint = 100;
		typeCombo.setLayoutData(gd);
		typeCombo.setEnabled(false);
		typeCombo.setItems(TYPE_ITEMS);

		final Label charsetLabel = new Label(container, SWT.NONE);
		gd = new GridData(GridData.FILL);
		charsetLabel.setText("Encoding :");
		gd.horizontalAlignment = GridData.BEGINNING;
		charsetLabel.setLayoutData(gd);

		// Encoding
		charsetCombo = new Combo(container, SWT.NONE);
		gd = new GridData(GridData.FILL);
		gd.horizontalSpan = 2;
		gd.widthHint = 100;
		charsetCombo.setItems(util.getEncodings());
		charsetCombo.setLayoutData(gd);

		final Label nameLabel = new Label(container, SWT.NONE);
		gd = new GridData(GridData.FILL);
		nameLabel.setText("Name :");
		nameLabel.setLayoutData(gd);
		
		// url name
		nameText = new Text(container, SWT.BORDER);
		gd = new GridData(GridData.FILL);
		gd.horizontalSpan = 2;
		gd.widthHint = 200;
		nameText.setLayoutData(gd);
		
		final Label urlLabel = new Label(container, SWT.NONE);
		gd = new GridData(GridData.FILL);
		urlLabel.setText("URL :");
		urlLabel.setLayoutData(gd);

		// url
		urlText = new Text(container, SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		gd.widthHint = 300;
		urlText.setLayoutData(gd);
		
		// test connection
		testConnectionButton = new Button(container, SWT.PUSH);
		gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		gd.horizontalSpan = 3;
		testConnectionButton.setText("Test Connection");
		testConnectionButton.setLayoutData(gd);
		
		initData();
		addControlEvent();
		return container;
	}
	
	private void addControlEvent() {
		testConnectionButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}

			public void widgetSelected(SelectionEvent e) {
				urlTestConnectionEvent();
			}
		});	
	}
	
	/**
	 * URL Test Connection Event
	 * @since
	 * @version
	 */
	private void urlTestConnectionEvent() {
		String sourcePath = urlText.getText();
		String result = "";
		try {
			result = util.urlConnectionTest(sourcePath);
		} catch(MalformedURLException e) {
			result = util.fileConnectionTest(sourcePath);
		} 
		MessageDialog.openConfirm(getShell(), "Connection Test", result);
	}

	protected void okPressed() {
		type = typeCombo.getText();
		charset = charsetCombo.getText();
		if(charset.equals("")) charset = DEFAULT_CHARSET;
		name = nameText.getText();
		url = urlText.getText();
		super.okPressed();
	}
}